package com.estar.transmit.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.estar.app.business.mobileLogin.utils.ObjCopyUtils;
import com.estar.platform.caseInfo.entity.TmblCarInfoEntity;
import com.estar.platform.caseInfo.entity.TmblCaseUpdateInfoEntity;
import com.estar.platform.caseInfo.entity.TmblSurveyInfoEntity;
import com.estar.transmit.vo.CarEntityVo;
import com.estar.transmit.vo.RequestVO;
import com.estar.transmit.vo.ReturnVO;
import com.estar.transmit.vo.SurveyEntityVo;

@Component("getSurveyList")
@Scope("prototype")
public class GetSurveyList {  
	
	private java.text.DateFormat df=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static final Logger logger = Logger.getLogger(GetSurveyList.class);
	private DateFormat dd=new SimpleDateFormat("yyyy-MM-dd");
	private DateFormat dt=new SimpleDateFormat("HH:mm:ss");
	private DateFormat dmonth=new SimpleDateFormat("MM");
	
	@Autowired
	private SystemService systemService;
	
	public ReturnVO getSurvey(RequestVO requestVO,Date startdate,Date enddate,String name){
		ReturnVO returnVo=new ReturnVO();
		List<SurveyEntityVo> surveyVOList=new ArrayList<SurveyEntityVo>();
		List<CarEntityVo> carVOList=new ArrayList<CarEntityVo>();
		try{
			CriteriaQuery cq = new CriteriaQuery(TmblSurveyInfoEntity.class);
			CriteriaQuery carCq = new CriteriaQuery(TmblCarInfoEntity.class);	
			
			String sql="select c.t_report_no from TMBL_CAR_INFO c where c.t_create_tm BETWEEN TO_DATE('"+df.format(startdate)+"', 'YYYY-MM-DD HH24:MI:SS') AND TO_DATE('"+df.format(enddate)+"', 'YYYY-MM-DD HH24:MI:SS')"+ 
						" and (c.t_compulsory_company='"+name+"' or c.t_Business_Company='"+name+"' )";

			List<String> reportNoList=this.systemService.findListbySql(sql);
			
			logger.info("接口调用=语句："+sql+";");
			logger.info("接口调用=开始时间："+startdate+";结束时间："+enddate+"||===");
			
			if(reportNoList.size()>0 && reportNoList!=null){
				String[] reports=new String[reportNoList.size()];
				for(int i=0;i<reportNoList.size();i++){
					reports[i]=reportNoList.get(i);
				}

				cq.in("reportNo", reports);
				cq.add();
				List<TmblSurveyInfoEntity> surveyList=systemService.getListByCriteriaQuery(cq, false);
				
				logger.info("开始时间："+startdate+";结束时间："+enddate+"；调用接口于,案件长度："+surveyList.size());
				
				carCq.in("reportNo", reports);
				carCq.add();
				List<TmblCarInfoEntity> carList=systemService.getListByCriteriaQuery(carCq, false);
				
				if(surveyList.size()>0 && surveyList!=null){
					for(TmblSurveyInfoEntity t :surveyList){
						SurveyEntityVo sv= new SurveyEntityVo();
						ObjCopyUtils.beansCopyFromJDBC(t, sv);
						if(t.getSpotCheckDate()!=null){
							sv.setSpotCheckDateT(df.format(t.getSpotCheckDate()));
						}
						if(t.getCreateTm()!=null){
							sv.setCreateTmT(df.format(t.getCreateTm()));
						}
						if(t.getUpdateTm()!=null){
							sv.setUpdateTmT(df.format(t.getUpdateTm()));
						}
						surveyVOList.add(sv);
					}
				}
				if(carList.size()>0 && carList!=null){
					for(TmblCarInfoEntity c :carList){
						CarEntityVo cv= new CarEntityVo();
						ObjCopyUtils.beansCopyFromJDBC(c, cv);
						if(c.getCreateTm()!=null){
							cv.setCreateTmT(df.format(c.getCreateTm()));
						}
						
						
						carVOList.add(cv);
					}
				}
			}
			
			//System.out.println("案件修改");
				Date today=new Date();
				String ri=dd.format(today);
				String sd=ri+" 23:00:00";
				Date sdd=df.parse(sd);
			if(today.compareTo(sdd)==1){
					
				String sql2="select c.t_report_no from TMBL_CAR_INFO c where c.t_create_tm BETWEEN TO_DATE('"+ri+" 00:00:00', 'YYYY-MM-DD HH24:MI:SS') AND TO_DATE('"+ri+" 23:59:59', 'YYYY-MM-DD HH24:MI:SS')"+ 
							" and (c.t_compulsory_company='"+name+"' or c.t_Business_Company='"+name+"' )";

				List<String> reportNoList2=this.systemService.findListbySql(sql2);
				
				logger.info("案件修改接口调用=语句："+sql2+";");
				
				if(reportNoList2.size()>0 && reportNoList2!=null){
					String[] reports2=new String[reportNoList2.size()];
					for(int i=0;i<reportNoList2.size();i++){
						reports2[i]=reportNoList2.get(i);
					}
					//修改案件的列表
					CriteriaQuery updatecq = new CriteriaQuery(TmblCaseUpdateInfoEntity.class);
					updatecq.setProjection(Property.forName("updateReportNo"));
					updatecq.isNotNull("expand");
					updatecq.in("updateReportNo", reports2);
					updatecq.add();
					List<String> updateReportNoList= systemService.getListByCriteriaQuery(updatecq, false);
					if(updateReportNoList!=null && updateReportNoList.size()>0){
						String[] updatereports=new String[updateReportNoList.size()];
						for(int i=0;i<updateReportNoList.size();i++){
							updatereports[i]=updateReportNoList.get(i);
						}
						CriteriaQuery cq2 = new CriteriaQuery(TmblSurveyInfoEntity.class);
						CriteriaQuery carCq2 = new CriteriaQuery(TmblCarInfoEntity.class);
						
						cq2.in("reportNo", updatereports);
						cq2.add();
						List<TmblSurveyInfoEntity> surveyList2=systemService.getListByCriteriaQuery(cq2, false);
						
						carCq2.in("reportNo", updatereports);
						carCq2.add();
						List<TmblCarInfoEntity> carList2=systemService.getListByCriteriaQuery(carCq2, false);
						
						if(surveyList2.size()>0 && surveyList2!=null){
							for(TmblSurveyInfoEntity t2 :surveyList2){
								SurveyEntityVo sv2= new SurveyEntityVo();
								ObjCopyUtils.beansCopyFromJDBC(t2, sv2);
								if(t2.getSpotCheckDate()!=null){
									sv2.setSpotCheckDateT(df.format(t2.getSpotCheckDate()));
								}
								if(t2.getCreateTm()!=null){
									sv2.setCreateTmT(df.format(t2.getCreateTm()));
								}
								if(t2.getUpdateTm()!=null){
									sv2.setUpdateTmT(df.format(t2.getUpdateTm()));
								}
								surveyVOList.add(sv2);
							}
						}
						if(carList2.size()>0 && carList2!=null){
							for(TmblCarInfoEntity c2 :carList2){
								CarEntityVo cv2= new CarEntityVo();
								ObjCopyUtils.beansCopyFromJDBC(c2, cv2);
								if(c2.getCreateTm()!=null){
									cv2.setCreateTmT(df.format(c2.getCreateTm()));
								}
								carVOList.add(cv2);
							}
						}
					}
				//
				}
			}
			returnVo.setCarVOList(carVOList);
			returnVo.setSurveyVOList(surveyVOList);
			returnVo.setUesrNme(requestVO.getUesrNme());
			returnVo.setResultCde("1");
			returnVo.setMessage("成功");
		}catch (Exception e) {
			logger.error("系统异常：",e);
			returnVo.setUesrNme("GetSurveyList出现异常");
			returnVo.setResultCde("0");
			returnVo.setMessage("GetSurveyList出现异常");
			e.printStackTrace();
		}
		return returnVo;
		
	}
	
	
	
}
